using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Pokemon
{
    class MainMenuScreen : GameScreen, IAnimation
    {
        private readonly Menu _menu;

        private TimeSpan _animationTime = TimeSpan.Zero;

        public MainMenuScreen(Game game)
            : base(game)
        {
            _menu = new Menu(this, @"MainMenu");

            var startGameMenuEntry = new Entry(_menu, @"New Game", new Vector2(30, 250));
            var exitMenuEntry = new Entry(_menu, @"Exit", new Vector2(30, 350));

            startGameMenuEntry.Selected += StartGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            startGameMenuEntry.Animated += PlayMenuEntryAnimation;
            exitMenuEntry.Animated += ExitMenuEntryAnimation;

            _menu.Add(startGameMenuEntry);
            _menu.Add(exitMenuEntry);

        }

        void StartGameMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.SwitchScreen("game" + @"play");
        }

        void OnCancel(object sender, EventArgs e)
        {
            Exit();
        }

        #region Ham thu ly Event Animation cho 2 menu entry

        double InitMenuEntryAnimation(Entry entry, TimeSpan animationTime, out SpriteBatch sp, out SpriteFont sf)
        {
            var progress = animationTime.TotalMilliseconds / 3000;
            if (progress > 1)
                progress = 1;
            sp = _screenManager.SpriteBatch;
            sf = _screenManager.SpriteFont;
            return progress;
        }

        void PlayMenuEntryAnimation(object sender, TimeSpan animationTime)
        {            
            Entry start = sender as Entry;
            if (start.IsStartup == false)
                return;
            SpriteBatch sp;
            SpriteFont sf;
            var progress = InitMenuEntryAnimation(start, animationTime, out sp, out sf);  
            
            ///Tam thoi luon cho progress = 1
            /// Mai mot se sua sau
            progress = 1;
            ///

            Vector2 position = start.Position;
            Vector2 realPosition = new Vector2((float) (position.X*progress), position.Y);
            start.Draw(sp, sf, realPosition, new Color(255, 255, 255, (int)(progress * 255)));
        }

        void ExitMenuEntryAnimation(object sender, TimeSpan animationTime)
        {
            Entry exit = sender as Entry;
            if (exit.IsStartup == false)
                return;
            SpriteBatch sp;
            SpriteFont sf;
            var progress = InitMenuEntryAnimation(exit, animationTime, out sp, out sf);

            ///Tam thoi luon cho progress = 1
            /// Mai mot se sua sau
            progress = 1;
            ///

            Vector2 position = exit.Position;
            Vector2 realPosition = new Vector2((float)(position.X * progress), position.Y);
            exit.Draw(sp, sf, realPosition, new Color(255, 255, 255, (int)(progress * 255)));
        }
#endregion




        public override void Draw(GameTime gameTime)
        {                      
            _menu.Draw(ScreenManager.SpriteBatch, ScreenManager.SpriteFont);            
        }

        public override void Update(GameTime gameTime)
        {
            _menu.Update();
        }
        #region IAnimation Members
        public bool IsStartup
        {
            get { return _isStartup; }
            set { _isStartup = value; }
        }

        public TimeSpan AnimationTime
        {
            get { return TimeSpan.Zero; }
            set { }
        }

        public override void Startup()
        {            
            _menu.Startup();            
        }

        public void Animation(SpriteBatch sp)
        {
        }
#endregion
    }
}
